<template lang="html">
  <div class="card" ref="hi">
    <div class="card-header" :style="{ height: height + 'px' }">
      <div class="img" v-show=imgVisible>
        <img :src="image" ref="test">
      </div>
      <div class="card-tips">
        <span>{{classify}}</span>
      </div>
    </div>
    <div class="card-article">
      <div class="card-title">
        <h3>{{title}}</h3>
      </div>
      <div class="card-content">
        <p>{{paragraph}}</p>
      </div>
    </div>
    <div class="card-footer">
      <div class="card-tip">
        <div class="avatar">
          <img :src="avatar">
        </div>
        <div class="author">
          <span>{{author}}</span>
          <span>·</span>
          <span>{{time}}天前</span>
        </div>
      </div>
      <div class="discuss">
        <div class="upvote">
          <i class="material-icons">favorite</i>
          <span>{{upvote}}</span>
        </div>
        <div class="comment">
          <i class="material-icons">chat</i>
          <span>{{upvote}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {

  props: {
    image: {type: String, default: ''},
    title: {type: String, default: ''},
    author: {type: String, default: ''},
    avatar: {type: String, default: ''},
    classify: {type: String, defauly: ''},
    paragraph: {type: String, default: ''},
    time: {type: [String, Number], default: ''},
    upvote: {type: [String, Number], default: ''}
  },
  data () {
    return {
      height: 160,
      imgVisible: false,
      timer: null
    }
  },
  methods: {
    imgLoad () {
      let temp = this.$refs.test
      this.height = (285 * temp.naturalHeight) / temp.naturalWidth
      if (this.height !== 0) {
        clearInterval(this.timer)
      }
    }
  },
  watch: {
    height () {
      if (this.height !== 0) {
        this.imgVisible = true
        this.$emit('loaded')
      }
    }
  },
  mounted () {
    this.timer = setInterval(this.imgLoad(), 50)
  }
}
</script>

<style lang="scss" scoped>

@mixin make-ellipse ($size) {
  width: $size;
  height: $size * 0.6;
  border-radius: $size * 0.6;
}
@mixin make-circle ($size) {
  width: $size;
  height: $size;
  border-radius: $size * 0.5;
}

.card {
  width: 305px;
  padding: 10px 10px 20px 10px;
  margin: 8px 8px;
  transition: box-shadow 500ms;
  background-color: #fff;
  &:hover {
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 15px 15px 0 rgba(15, 37, 64, 0.10);
    transition: box-shadow 500ms;
  }

  .card-header {
    position: relative;

    width: 285px;
    width: 100%;
    cursor: pointer;
    background-color: #eee;

    .img {

      width: 285px;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .card-tips {
      @include make-ellipse (40px);
      position: absolute;
      top: 10px;
      left: 10px;

      display: flex;
      align-items: center;
      justify-content: center;

      color: #eee;
      font-size: 14px;
      cursor: pointer;
      text-align: center;
      background-color: rgba(0, 0, 0, .4);

      &:hover {
        background-color: rgba(0, 0, 0, .5);
      }
    }

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .card-article {
    width: 285px;
    width: 100%;
    padding: 14px 10px 0px 10px;

    .card-title {
      margin-bottom: 10px;
      cursor: pointer;
      h3 {
        font-size: 16px;
        line-height: 1.5;
      }
    }

    .card-content {
      width: 100%;

      p {
        width: 265px;
        height: 42px;
        line-height: 1.5;
        overflow: hidden;
        word-wrap:break-word;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        color: #999;
        font-size: 14px;
      }
    }
  }

  .card-footer {
      width: 285px;
      height: 88px;
      .card-tip {

        margin: 0 auto;
        height: 48px;
        margin-left: 10px;
        padding-top: 14px;
        font-size: 14px;
        display: flex;
        align-items: center;

        .avatar {
          @include make-circle (24px);
          overflow: hidden;
          display: inline-flex;
          margin-right: 10px;
          cursor: pointer;
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        .author{
          color: #666;
          font-weight: bold;
          display: inline-flex;
          align-items: center;
          cursor: pointer;
          span:nth-child() {
            width: 17px;
            padding: 0px 5px;
            cursor: pointer;
          }
          span:nth-child(2) {
            width: 17px;
            padding: 0px 5px;
          }
          span:nth-child(3) {
            color: #999;
            font-size: 12px;
          }
        }
      }
      .discuss {
        width: 100%;
        height: 40px;
        padding-bottom: 20px;
        margin-top: 4px;
        margin-left: 10px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .upvote {
          color: #999;
          display: flex;
          align-items: center;
          margin-right: 10px;
          span {
            font-size: 14px;
          }
          i {
            font-size: 16px;
            margin-right: 5px;
            cursor: pointer;
          }
        }

        .comment {
          color: #999;
          display: inline-flex;
          span {
            font-size: 14px;
          }
          i {
            font-size: 16px;
            margin-right: 5px;
            cursor: pointer;
          }
        }
      }
    }
}

</style>
